package com.ikkong.common.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import sun.misc.BASE64Decoder;

public class CommonUtils {
	   public static boolean GenerateImage(String imgStr, String imgFilePath) {// 对字节数组字符串进行Base64解码并生成图片
	        if (imgStr == null) // 图像数据为空
	            return false;
	        BASE64Decoder decoder = new BASE64Decoder();
	        OutputStream out =null;
	        try {
	            // Base64解码
	            byte[] bytes = decoder.decodeBuffer(imgStr);
	            for (int i = 0; i < bytes.length; ++i) {
	                if (bytes[i] < 0) {// 调整异常数据
	                    bytes[i] += 256;
	                }
	            }

	            out = new FileOutputStream(imgFilePath);
	            out.write(bytes);
	            out.flush();
	           
	            return true;
	        } catch (Exception e) {
	        	System.out.print(e.getMessage());
	            return false;
	        }finally{
	        	 try {
					out.close();
				} catch (IOException e) {
					System.out.print(e.getMessage());
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	    }
	   
	   /**
	     * 单个对象的所有键值
	     * 
	     * @param object
	     *            单个对象
	     * 
	     * @return Map<String, Object> map 所有 String键 Object值 ex：{pjzyfy=0.00,
	     *         xh=01, zzyl=0.00, mc=住院患者压疮发生率, pjypfy=0.00, rs=0, pjzyts=0.00,
	     *         czydm=0037, lx=921, zssl=0.00}
	     */
	    public static Map<String, Object> getKeyAndValue(Object obj) {
	        Map<String, Object> map = new HashMap<String, Object>();
	        // 得到类对象
	        Class userCla = (Class) obj.getClass();
	        /* 得到类中的所有属性集合 */
	        Field[] fs = userCla.getDeclaredFields();
	        for (int i = 0; i < fs.length; i++) {
	            Field f = fs[i];
	            f.setAccessible(true); // 设置些属性是可以访问的
	            Object val = new Object();
	            try {
	                val = f.get(obj);
	                // 得到此属性的值
	                map.put(f.getName(), val);// 设置键值
	            } catch (IllegalArgumentException e) {
	                e.printStackTrace();
	            } catch (IllegalAccessException e) {
	                e.printStackTrace();
	            }

	            /*
	             * String type = f.getType().toString();//得到此属性的类型 if
	             * (type.endsWith("String")) {
	             * System.out.println(f.getType()+"\t是String"); f.set(obj,"12") ;
	             * //给属性设值 }else if(type.endsWith("int") ||
	             * type.endsWith("Integer")){
	             * System.out.println(f.getType()+"\t是int"); f.set(obj,12) ; //给属性设值
	             * }else{ System.out.println(f.getType()+"\t"); }
	             */

	        }
	        System.out.println("单个对象的所有键值==反射==" + map.toString());
	        return map;
	    }
	    
	    /**
		 * 比较版本号的大小,前者大则返回一个正数,后者大返回一个负数,相等则返回0
		 * @param version1
		 * @param version2
		 * @return
		 */
		public static int compareVersion(String version1, String version2) throws Exception {
			if (version1 == null || version2 == null) {
				throw new Exception("compareVersion error:illegal params.");
			}
			String[] versionArray1 = version1.split("\\.");//注意此处为正则匹配，不能用"."；
			String[] versionArray2 = version2.split("\\.");
			int idx = 0;
			int minLength = Math.min(versionArray1.length, versionArray2.length);//取最小长度值
			int diff = 0;
			while (idx < minLength
					&& (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0//先比较长度
					&& (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) {//再比较字符
				++idx;
			}
			//如果已经分出大小，则直接返回，如果未分出大小，则再比较位数，有子版本的为大；
			diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length;
			return diff;
		}

}
